package objects;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import common.GameServer;
import common.Maths;

public class Fight
{

	private ArrayList<Object> teamOne = new ArrayList<Object>();
	private ArrayList<Object> teamTwo = new ArrayList<Object>();
	private ArrayList<Short> usedPlaces = new ArrayList<Short>();
	
	private boolean allowSpectator, needHelp, isLocked, groupOnly;
	private long guid, ownerID, time;
	private short cellID;
	private byte type;
	private Timer timer;
	
	private static long instanceInc = 0;
	
	public Fight()
	{
		
	}
	
	public Fight(Character pOwner, byte pType, short pCellID)
	{
		instanceInc++;
		
		guid = instanceInc;
		
		pOwner.setFighting(true);
		pOwner.setFightID(guid);
		
		ownerID = pOwner.getEntitieMapGuid();
		type = pType;
		cellID = pCellID;
		
		allowSpectator = true;
		needHelp = false;
		isLocked = false;
		groupOnly = false;
		
		time = 0;
		timer = new Timer();
		timer.schedule(new TimerTask(){
			public void run()
			{
				time++;
			}
		}, 1000);
		
		GameServer.writeLine("New fight created (Guid : " + guid + ")");
	}

	public ArrayList<Object> getTeamOne() {
		return teamOne;
	}

	public void addTeamOne(Object entitie) {
		this.teamOne.add(entitie);
	}

	public ArrayList<Object> getTeamTwo() {
		return teamTwo;
	}

	public void setTeamTwo(Object entitie) {
		this.teamTwo.add(entitie);
	}

	public boolean isAllowSpectator() {
		return allowSpectator;
	}

	public void setAllowSpectator(boolean allowSpectator) {
		this.allowSpectator = allowSpectator;
	}

	public boolean isNeedHelp() {
		return needHelp;
	}

	public void setNeedHelp(boolean needHelp) {
		this.needHelp = needHelp;
	}

	public boolean isLocked() {
		return isLocked;
	}

	public void setLocked(boolean isLocked) {
		this.isLocked = isLocked;
	}

	public boolean isGroupOnly() {
		return groupOnly;
	}

	public void setGroupOnly(boolean groupOnly) {
		this.groupOnly = groupOnly;
	}

	public long getGuid() {
		return guid;
	}

	public void setGuid(long guid) {
		this.guid = guid;
	}

	public short getCellID() {
		return cellID;
	}

	public void setCellID(short cellID) {
		this.cellID = cellID;
	}

	public long getOwnerID() {
		return ownerID;
	}

	public void setOwnerID(long ownerID) {
		this.ownerID = ownerID;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}
	
	public short getRandomPlace(Map map, byte team)
	{
		short place;
		if(team == 1)
		{
			do
			{
				place = map.getPlacesTeamOne().get(Maths.randomBetween(0, map.getPlacesTeamOne().size()-1));
			}while(usedPlaces.contains(place));
			usedPlaces.add(place);
			
			return place;
		}
		else if(team == 2)
		{
			do
			{
				place = map.getPlacesTeamTwo().get(Maths.randomBetween(0, map.getPlacesTeamTwo().size()-1));
			}while(usedPlaces.contains(place));
			usedPlaces.add(place);
			
			return place;
		}
		else
		{
			return 0;
		}
	}

}